package tech.aistar.corejava.day12.homework02;

import tech.aistar.corejava.util.DateUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author success
 * @version 1.0
 * @description:本类用来演示:
 * @date 2019/8/1 0001
 */
public class UserDaoImpl implements IUserDao{
   private List<TransRecord> list = new ArrayList<>();

    //构造块
    public UserDaoImpl(){
        //java中看到0打头的代表是八进制.
        list.add(new TransRecord(1,"刘德华",0000,1,"4155990188888888","20060720200005l",300.00));
        list.add(new TransRecord(201,"晓龙",0002,1,"4155990199999999","20060720200005",500.00));
        list.add(new TransRecord(101,"黄晓明",0012,1,"4155990100000000","20060720200005",1000.50));
        list.add(new TransRecord(101,"张东健",0012,1,"4155990155555555","20060720200005",600.99));
        list.add(new TransRecord(301,"梁朝伟",0013,0,"4155990111111111","20060722201005",5000.00));
        list.add(new TransRecord(1,"刘德华",0000,1,"4155990188888888","20060725200005",200.00));
    }

    @Override
    public List<TransRecord> findByCustomerNumber(String customerNumber) {

       // System.out.println(list);

        List<TransRecord> records = new ArrayList<>();//存放满足条件的

        for (int i = 0; i < list.size(); i++) {
            //一定要注意类型一定要相同除了包装类型和基本类型.
            if(String.valueOf(list.get(i).getUid()).equals(customerNumber)){
                records.add(list.get(i));
            }
        }

        //JDK8.0关于集合的新增的API
        //java.util.stream.Stream
        return records;
    }

    @Override
    public List<TransRecord> findByDate(String startDate, String endDate) {
        List<TransRecord> records = new ArrayList<>();
        //将日期格式的字符串转换成Date类型
        Date start = DateUtil.parse(startDate,"yyyyMMddHHmmss");
        Date end = DateUtil.parse(endDate,"yyyyMMddHHmmss");

        for (TransRecord transRecord : list) {
            Date createDate = DateUtil.parse(transRecord.getTime(),"yyyyMMddHHmmss");

            //第一种方式 - 日期转换成毫秒数比较  start.getTime()  end.getTime() createDate.getTime()
//            if((createDate.after(start) && createDate.before(end)) || createDate.equals(start) || createDate.equals(end)){
//                records.add(transRecord);
//            }

            if(createDate.getTime()>=start.getTime() && createDate.getTime()<=end.getTime()){
                records.add(transRecord);
            }
        }

        return records;
    }

    @Override
    public BigDecimal totalAmount() {
        double total = 0.0D;
        for (TransRecord transRecord : list) {
            total += transRecord.getPrice();
        }
        return new BigDecimal(total);
    }
}
